package com.yanqu.road.server.manger.activity.gashapon;

import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.eActivityConfigType;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.logic.bussiness.activity.GashaponBusiness;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.gashapon.GashaponModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;

import java.util.List;
import java.util.Map;

/**
 * @Author : Cxp
 * @Time : 2024-10-21
 * @Description : 幸运扭蛋Mgr
 */

public class GashaponMgr extends TempMgr {

    static GashaponConfig config;

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return false;
    }

    public static void reloadActivityData() {
        getLogger().info("Reload gashapon activity start!");

        List<ActivityInfo> openActivityInfoList = NormalActivityMgr.getOpenActivityInfoList(eActivityType.Gashapon.getValue()); // 加载开放的活动
        if (openActivityInfoList.isEmpty()) {
            getLogger().info("No gashapon activity in show time!");
            config = null;
        } else {
            ActivityInfo activityInfo = openActivityInfoList.get(0);
            int activityId = activityInfo.getActivityId();
            GashaponConfig gashaponConfig = new GashaponConfig(activityInfo);
            Map<String, ActivityConfig> configMap = ActivityMgr.getActivityConfigMap(activityId); // 固定配置表t_s_activity_config

            if (configMap != null) {
                gashaponConfig.setOneDrawCost(configMap.get(eActivityConfigType.GASHAPON_ONE_LOTTERY_COST.getName()).getValue());
                gashaponConfig.setOneDrawScore(configMap.get(eActivityConfigType.GASHAPON_ONE_LOTTERY_SCORE_GET.getName()).getIntValue());
                gashaponConfig.setGuaranteeNum(configMap.get(eActivityConfigType.GASHAPON_LOTTERY_MAX_NUM.getName()).getIntValue());
                gashaponConfig.setHiddenGuaranteeNum(configMap.get(eActivityConfigType.GASHAPON_LOTTERY_BEST_MAX_NUM.getName()).getValue());
            }
            gashaponConfig.setRewardMap(GashaponBusiness.getRewardConfig(activityId));

            config = gashaponConfig;
        }

        for (GamePlayer player : GamePlayerMgr.getAllOnlinePlayer()) {
            player.getModule(GashaponModule.class).activityReload();
        }

        getLogger().info("Reload gashapon activity finish!");
    }

    public static GashaponConfig getConfig() {
        return config;
    }

    public static ActivityInfo getActivityInfo() {
        if (config == null) {
            return null;
        }
        return config.getActivityInfo();
    }
}
